/*
 * $Id$
 *
 * Copyright 2008 HuangHuang. All rights reserved.
 */
package com.crm.service.impl;

import com.crm.service.ICustomerService;
import com.crm.service.ISloggerService;
import com.crm.service.utils.ServiceUtils;
import com.crm.dao.ICustomerDao;
import com.crm.model.Customer;
import com.crm.model.User;
import com.crm.model.SysLog;

import java.util.List;
import java.util.Calendar;
import java.util.Date;

/**
 * 客户信息管理的逻辑处理服务类。
 *
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public class CustomerService implements ICustomerService {

    private static final String INF_CRE = "Create Customer";
    private static final String INF_DEL = "Delete Customer";
    private static final String INF_UPD = "Update Customer";
    enum Color {RED, GREEN, BLUE}
    private ICustomerDao customerDao;
    private ISloggerService slogServ;

    public void setCustomerDao(ICustomerDao customerDao) {
        this.customerDao = customerDao;
    }


    public void setSlogServ(ISloggerService slogServ) {
        this.slogServ = slogServ;
    }

    /**
     * {@inheritDoc}
     */
    public void create(Customer customer) {
        customerDao.save(customer);
        slogServ.create(customer.getUser(), SysLog.T_CUST, SysLog.ACT_C, INF_CRE);
    }

    /**
     * {@inheritDoc}
     */
    public void update(Customer customer) {
        customerDao.save(customer);
        slogServ.create(customer.getUser(), SysLog.T_CUST, SysLog.ACT_U, INF_UPD);
    }

    /**
     * {@inheritDoc}
     */
    public List getCustomers() {
        return customerDao.getCustomerAll();
    }

    /**
     * {@inheritDoc}
     */
    public String getSameName(String inputName) {
        List customerNameList = customerDao.getCustomerNameAll();
        String nameInDB;
        for (Object aCustomerNameList : customerNameList) {
            nameInDB = (String) aCustomerNameList;
            if (ServiceUtils.convert(nameInDB).equals(ServiceUtils.convert(inputName))) {
                return nameInDB;
            }
        }
        customerNameList.clear();
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public String getSameNameExcludeSelf(String id, String name) {
        List customerNameList = customerDao.getCustomerNameExclude(id);
        String nameInDB;
        for (Object aCustomerNameList : customerNameList) {
            nameInDB = (String) aCustomerNameList;
            if (ServiceUtils.convert(nameInDB).equals(ServiceUtils.convert(name))) {
                return nameInDB;
            }
        }
        customerNameList.clear();
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public Customer getCustomerById(String customerId) {
        return customerDao.getCustomerById(customerId);
    }

    /**
     * {@inheritDoc}
     */
    public List getCustomers(User user) {
        checkUser(user);
        if (User.GROUP_SUPER.equals(user.getUserGroup())) {
            return customerDao.getCustomerAll();
        } else {
            return customerDao.getCustomersByUserCode(user.getUserCode());
        }
    }

    /**
     * {@inheritDoc}
     */
    public List getCustomerThisWeek(User user) {
        checkUser(user);
        String weekOfYear = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
        weekOfYear += String.valueOf(Calendar.getInstance().get(Calendar.WEEK_OF_YEAR));
        return getCustomerByWeek(user, weekOfYear);
    }

    /**
     * {@inheritDoc}
     */
    public List getCustomerLastWeek(User user) {
        checkUser(user);
        String weekOfYear = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
        int lastWeek = Calendar.getInstance().get(Calendar.WEEK_OF_YEAR) - 1;
        if (lastWeek == 0) {
            weekOfYear = String.valueOf(Integer.parseInt(weekOfYear) - 1);
            lastWeek = 52;
        }
        weekOfYear = weekOfYear + String.valueOf(lastWeek);
        return getCustomerByWeek(user, weekOfYear);
    }

    /**
     * {@inheritDoc}
     */
    public List getCustomers(User user, String[] searchParam, Date[] dates) {
        checkUser(user);
        if (searchParam.length == 7) {
            return advacedQuery(user, searchParam, dates);
        }
        List list;
        String info = "search customer without keywords.";
        if (searchParam[0] != null && searchParam[1].equals("客户名称")) {
            list = customerDao.getCustomersByKeyword(user.getUserGroup(),
                    user.getUserCode(), searchParam[0], "", "", "", "", "", "", ServiceUtils.MIN_DATE, ServiceUtils.MAX_DATE);
            info = "search customer by name. keyword: " + searchParam[0];
        } else if (searchParam[0] != null && searchParam[1].equals("国家")) {
            list = customerDao.getCustomersByKeyword(user.getUserGroup(),
                    user.getUserCode(), "", searchParam[0], "", "", "", "", "", ServiceUtils.MIN_DATE, ServiceUtils.MAX_DATE);
            info = "search customer by country. keyword: " + searchParam[0];
        } else if (searchParam[0] != null && searchParam[1].equals("网址")) {
            list = customerDao.getCustomersByKeyword(user.getUserGroup(),
                    user.getUserCode(), "", "", searchParam[0], "", "", "", "", ServiceUtils.MIN_DATE, ServiceUtils.MAX_DATE);
            info = "search customer by website. keyword: " + searchParam[0];
        } else if (searchParam[0] != null && searchParam[1].equals("产品")) {
            list = customerDao.getCustomersByKeyword(user.getUserGroup(),
                    user.getUserCode(), "", "", "", searchParam[0], "", "", "", ServiceUtils.MIN_DATE, ServiceUtils.MAX_DATE);
            info = "search customer by business type. keyword: " + searchParam[0];
        } else {
            list = customerDao.getCustomersByKeyword(user.getUserGroup(), user.getUserCode(), "", "", "", "", "", "", "", ServiceUtils.MIN_DATE, ServiceUtils.MAX_DATE);
        }
        if (info.indexOf("without") == -1) {
            slogServ.create(user, SysLog.T_CUST, SysLog.ACT_R, info);
        }
        return list;
    }

    private List advacedQuery(User user, String[] searchParam, Date[] dates) {
        return customerDao.getCustomersByKeyword(user.getUserGroup(),
                user.getUserCode(), searchParam[0], searchParam[1],
                searchParam[2], searchParam[3], searchParam[4], searchParam[5],
                searchParam[6],dates[0], dates[1]);
    }

    /**
     * {@inheritDoc}
     */
    public void delete(Customer customer, User user) {
        customerDao.delete(customer);
        slogServ.create(user, SysLog.T_CUST, SysLog.ACT_D, INF_DEL);
    }

    /**
     * {@inheritDoc}
     */
    public String getSameWebsite(String website) {
        List customerWebsiteList = customerDao.getCustomerWebsite();
        String websiteInDB;
        for (Object aCustomerWebsiteList : customerWebsiteList) {
            websiteInDB = (String) aCustomerWebsiteList;
            if (ServiceUtils.convert(websiteInDB).equals(ServiceUtils.convert(website))) {
                return websiteInDB;
            }
        }
        customerWebsiteList.clear();
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public String getSameWebsiteExcludeSelf(String id, String website) {
        List customerWebsiteList = customerDao.getCustomerWebsiteExclude(id);
        String websiteInDB;
        for (Object aCustomerWebsiteList : customerWebsiteList) {
            websiteInDB = (String) aCustomerWebsiteList;
            if (ServiceUtils.convert(websiteInDB).equals(ServiceUtils.convert(website))) {
                return websiteInDB;
            }
        }
        customerWebsiteList.clear();
        return null;
    }

    public List getRecycleCust(String uid) {
        return customerDao.getRecycleCust(uid);
    }

    private void checkUser(User user) {
        if (user.getUsername() == null || user.getPassword() == null) {
            throw new NullPointerException("User session is null!");
        }
    }

    private List getCustomerByWeek(User user, String weekOfYear) {
        String ug = user.getUserGroup();
        if (User.GROUP_SUPER.equals(ug) || User.GROUP_ADMIN.equals(ug)) {
            return customerDao.getCustomerByWeek(weekOfYear);
        } else {
            return customerDao.getCustomerByWeek(user.getUserCode(), weekOfYear);
        }
    }
}

